package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponExample;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.MarketCouponUserExample;
import com.cskaoyan.bean.bo.AdminCouponListBO;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Fyy
 * @since 2022/12/29 15:33
 */

@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    MarketCouponMapper couponMapper;
    @Autowired
    MarketCouponUserMapper couponUserMapper;

    @Override
    public CommonData<MarketCoupon> queryAll(AdminCouponListBO adminCouponListBO) {
        //分页
        PageHelper.startPage(adminCouponListBO.getPage(),adminCouponListBO.getLimit());
        //给查询所需参数进行赋值
        MarketCouponExample example = new MarketCouponExample();
        example.setOrderByClause(adminCouponListBO.getSort()+" "+adminCouponListBO.getOrder());
        MarketCouponExample.Criteria criteria = example.createCriteria();
        //对参数判空
        if (!StringUtil.isEmpty(adminCouponListBO.getName())) {
            criteria.andNameLike("%" + adminCouponListBO.getName() + "%");
        }
        if (!StringUtil.isEmpty(adminCouponListBO.getType())) {
            criteria.andTypeEqualTo(adminCouponListBO.getType());
        }
        if (!StringUtil.isEmpty(adminCouponListBO.getStatus())) {
            criteria.andStatusEqualTo(adminCouponListBO.getStatus());
        }
        criteria.andDeletedEqualTo(false);
        //获取查询结果
        List<MarketCoupon> list = couponMapper.selectByExample(example);
        PageInfo<MarketCoupon> marketCouponPageInfo = new PageInfo<>(list);
        return CommonData.data(marketCouponPageInfo);
    }

    @Override
    public void create(MarketCoupon marketCoupon) {
        couponMapper.insertSelective(marketCoupon);
    }

    @Override
    public void update(MarketCoupon marketCoupon) {
        couponMapper.updateByPrimaryKeySelective(marketCoupon);
    }

    @Override
    public void delete(MarketCoupon marketCoupon) {
        MarketCoupon realMarketCoupon = new MarketCoupon();
        realMarketCoupon.setId(marketCoupon.getId());
        realMarketCoupon.setDeleted(true);
        couponMapper.updateByPrimaryKeySelective(realMarketCoupon);
    }

    @Override
    public MarketCoupon query(Integer id) {
        MarketCoupon marketCoupon = couponMapper.selectByPrimaryKey(id);
        return marketCoupon;
    }

    @Override
    public CommonData<MarketCouponUser> listUser(BasePageInfo basePageInfo, Integer couponId, Integer userId, Short status) {
        //分页
        PageHelper.startPage(basePageInfo.getPage(),basePageInfo.getLimit());
        //对查询所需参数进行赋值
        MarketCouponUserExample example = new MarketCouponUserExample();
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        if (couponId != null) {
            criteria.andCouponIdEqualTo(couponId);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);
        //获取查询得到的值
        List<MarketCouponUser> list = couponUserMapper.selectByExample(example);
        PageInfo<MarketCouponUser> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }
}
